package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    //新增商机跟进记录
    @Override
    public void addBusinessTrackRecord(BusinessTrackVo businessTrackVo) {
        //商机跟进表
        TbBusinessTrackRecord tbBusinessTrackRecord=new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        //商机跟进表
        TbBusiness tbBusiness=new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusiness.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());
        //商机跟进表-添加
        tbBusinessTrackRecordMapper.insertTbBusinessTrackRecord(tbBusinessTrackRecord);
        //商机表-修改
        tbBusinessMapper.updateTbBusiness(tbBusiness);
    }
    //列表--商机跟进记录
    @Override
    public List<BusinessTrackVo> listBusinessTrack(Long id) {
        List<BusinessTrackVo> businessTrackVoList=tbBusinessTrackRecordMapper.listBusinessTrack(id);
        businessTrackVoList=businessTrackVoList.stream().map(item->{
            if(item.getKeyItems()!=null){
                String[] strings = item.getKeyItems().split(",");//[2,3,4]  数字查字典
                for (String string : strings) {
                    String communication_point = dictDataMapper.selectDictLabel("communication_point", string);
                    item.getKeys().add(communication_point);
                }
            }
            return item;
        }).collect(Collectors.toList());
        return businessTrackVoList;
    }

}
